package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqWaterProductionInspectionEquipmentMapper;
import com.guofen.pojo.SjqWaterProductionInspectionEquipment;
import com.guofen.service.sjq.SjqWaterProductionInspectionEquipmentService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqWaterProductionInspectionEquipmentServiceImpl implements SjqWaterProductionInspectionEquipmentService {

    @Autowired
    private SjqWaterProductionInspectionEquipmentMapper sjqWaterProductionInspectionEquipmentMapper;

    @Override
    public boolean addForm(String equipment_name, String is_normal, String operator, String inspector, String remarks) {
        //获取当前日期
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String date = dateFormat.format(now);
        String time = timeFormat.format(now);

        SjqWaterProductionInspectionEquipment sjqWaterProductionInspectionEquipment = new SjqWaterProductionInspectionEquipment();
        sjqWaterProductionInspectionEquipment.setDate(date);
        sjqWaterProductionInspectionEquipment.setTime(time);
        sjqWaterProductionInspectionEquipment.setEquipmentName(equipment_name);
        sjqWaterProductionInspectionEquipment.setIsNormal(is_normal);
        sjqWaterProductionInspectionEquipment.setOperator(operator);
        sjqWaterProductionInspectionEquipment.setInspector(inspector);
        sjqWaterProductionInspectionEquipment.setRemarks(remarks);

        int result = sjqWaterProductionInspectionEquipmentMapper.insert(sjqWaterProductionInspectionEquipment);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public List<SjqWaterProductionInspectionEquipment> findForm() {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        System.out.println("当前日期" + date);
        //获取最新记录
        List<SjqWaterProductionInspectionEquipment> sjqWaterProductionInspectionEquipmentList = sjqWaterProductionInspectionEquipmentMapper.findLast();
        return sjqWaterProductionInspectionEquipmentList;
    }

    @Override
    public boolean updateForm(int id, String inspector, String remarks) {
        SjqWaterProductionInspectionEquipment sjqWaterProductionInspectionEquipment = new SjqWaterProductionInspectionEquipment();
        sjqWaterProductionInspectionEquipment.setId(id);
        sjqWaterProductionInspectionEquipment.setInspector(inspector);
        sjqWaterProductionInspectionEquipment.setRemarks(remarks);

        int result = sjqWaterProductionInspectionEquipmentMapper.updateByPrimaryKeySelective(sjqWaterProductionInspectionEquipment);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqWaterProductionInspectionEquipment> list = sjqWaterProductionInspectionEquipmentMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqWaterProductionInspectionEquipment> list = sjqWaterProductionInspectionEquipmentMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqWaterProductionInspectionEquipmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqWaterProductionInspectionEquipment record) {
        return sjqWaterProductionInspectionEquipmentMapper.updateByPrimaryKeySelective(record);
    }
}
